//百度地图AK设置模块
SUCCESS_TEXT = "设置成功。";
FAILURE_TEXT = "设置失败。";
EMPTY_TEXT = "不能为空。";

function set_ak_tip(success, content) {
    const target = document.getElementById("setak_tip");
    if (success === true) {
        target.classList.remove("text-danger");
        target.classList.add("text-success");
    } else {
        target.classList.remove("text-success");
        target.classList.add("text-danger");
    }
    target.innerText = content;
    target.style.display = "block";
}

function submit_general() {
    let new_ak = document.getElementById("akbox").value;
    let new_channel = document.getElementById("def_chan").value;
    let new_serverS = document.getElementById("serverS").value;
    let new_serverC = document.getElementById("serverC").value;
    fetch('/api/general', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ 
            ak: new_ak, 
            channel: new_channel,
            serverS: new_serverS,
            serverC: new_serverC
        })
    }).then(response => response.json())
    .then(data => {
        if (data.success === true) {
            set_ak_tip(true, SUCCESS_TEXT);
        } else {
            throw new Error();
        }
    }).catch((error) => {
        set_ak_tip(false, FAILURE_TEXT);
    });
}